package com.fdb.efp.esb.service.impl.psd;

import com.alibaba.fastjson.JSONObject;
import com.fdb.efp.esb.common.client.resp.EsbRespServiceBean;
import com.fdb.efp.esb.common.constant.ESBClientConstance;
import com.fdb.efp.esb.common.constant.EsbBizEnums;
import com.fdb.basic.framework.core.constant.ESBConstant;
import com.fdb.basic.framework.core.exception.ESBException;
import com.fdb.efp.esb.service.bo.req.psd.PsdCreditApprReqBean;
import com.fdb.efp.esb.service.bo.resp.psd.PsdCreditApprRespBean;
import com.fdb.efp.esb.service.client.ESBRequestBuilder;
import com.fdb.efp.esb.service.client.EsbBaseBean;
import com.fdb.efp.esb.service.facade.psd.PsdCreditApprService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service("psdCreditApprService")
public class PsdCreditApprServiceImpl implements PsdCreditApprService {
    private static Logger logger = LoggerFactory.getLogger(PsdCreditApprServiceImpl.class);

    @Override
    public PsdCreditApprRespBean applyPsdCreditAppr(PsdCreditApprReqBean psdCreditApprReqBean) throws ESBException {
        logger.info("======>调用风控普税贷授信审批接口【" + psdCreditApprReqBean.getQryRptsInfArry().get(0).getAppNo() + "_30220001_18】开始<======");
        PsdCreditApprRespBean psdCreditApprRespBean = null;
        try {
            EsbBaseBean esbBaseBean = EsbBaseBean.EsbBaseBeanBuilder.
                    anEsbBaseBean().
                    withINSTID(ESBClientConstance.HPJRBORGID).withGLBLSRLNO(psdCreditApprReqBean.getGolSeq()).build();
            EsbRespServiceBean recvBean = ESBRequestBuilder.buildClient().
                    withEsbBaseBean(esbBaseBean)
                    .withBody(psdCreditApprReqBean)
                    .withTradeNo(ESBConstant.TRADE_CODE_30220001)
                    .withScene(ESBConstant.SCENE_NO_18)
                    .create()
                    .sendAndReceive();
            if (EsbBizEnums.RetCodeEnum.SUCCESS.VALUE.equals(recvBean.getSysHead().getRetInfArry().get(0).getRetCd())) {
                psdCreditApprRespBean = recvBean.getBody(PsdCreditApprRespBean.class);
            } else {
                throw new ESBException(recvBean.getSysHead().getRetInfArry().get(0).getRetMsg());
            }
            logger.info("调用风控普税贷授信审批接口【30220001_18】结束，返回信息：" + JSONObject.toJSONString(psdCreditApprRespBean));
        } catch (Exception e) {
            logger.error("调用风控普税贷授信审批接口【30220001_18】出现异常：" + e.getMessage());
            throw new ESBException("调用风控普税贷授信审批接口【30220001_18】出现异常：" + e.getMessage());
        } finally {
            logger.info("======>调用风控普税贷授信审批接口【" + psdCreditApprReqBean.getQryRptsInfArry().get(0).getAppNo() + "_30220001_18】结束<======");
        }
        return psdCreditApprRespBean;
    }
}